package com.lbm.business.web.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import com.lbm.baseclass.controller.LBMBaseController;
import com.lbm.baseclass.utils.json.JsonUtil;
import com.lbm.business.service.IRoleInfoService;
import com.lbm.business.service.IRoleModuleService;
import com.lbm.model.RoleInfo;
import com.lbm.model.RoleModule;

public class RoleInfoController extends LBMBaseController{
	private IRoleInfoService roleInfoService;
	private IRoleModuleService roleModuleService;

	public void setRoleModuleService(IRoleModuleService roleModuleService) {
		this.roleModuleService = roleModuleService;
	}
	public void setRoleInfoService(IRoleInfoService roleInfoService) {
		this.roleInfoService = roleInfoService;
	}
	/**
	* 角色列表
	* @param request
	* @param response
	* @return
	* @throws Exception
	*/
	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv=new ModelAndView("WEB-INF/jsp/roleinfo/roleinfolist.jsp");
	    return mv;
	}
	/**
	* 加载角色列表数据
	* @param request
	* @param response
	* @return
	* @throws Exception
	*/
	public ModelAndView ajaxlist(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String hql="from RoleInfo where 1=1";
		String sort=request.getParameter("sort");
		String order=request.getParameter("order");
		if(sort!=""&&sort!=null)hql+=" order by "+sort+" "+order;
		List<RoleInfo> list=calcPage(request, roleInfoService, hql);
		int total=roleInfoService.getAllRoleInfo().size();
		String json="";
		json = "{\"total\": " + total + ", \"rows\": "
				+ JsonUtil.toString(list, JsonUtil.COLLECTION_FILTER) + "}";
		renderJson(response, json);
	    return null;
	}
	/**
	* 添加
	* @param request
	* @param response
	* @return
	* @throws Exception
	*/
	public ModelAndView add(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv=new ModelAndView("WEB-INF/jsp/roleinfo/roleinfoform.jsp");
		return mv;
	}
	/**
	* 修改
	* @param request
	* @param response
	* @return
	* @throws Exception
	*/
	public ModelAndView modify(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv=new ModelAndView("WEB-INF/jsp/roleinfo/roleinfoform.jsp");
		RoleInfo roleInfo=roleInfoService.getRoleInfoById(request.getParameter("id"));
		mv.addObject("roleInfo", roleInfo);
		return mv;
	}
	/**
	* 删除
	* @param request
	* @param response
	* @return
	* @throws Exception
	*/
	public ModelAndView delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		boolean flag=true;
		String id=request.getParameter("ids");
		String[] ids=id.split(",");
		for(int i=0;i<ids.length;i++){
			try {
				RoleInfo user = roleInfoService.getRoleInfoById(ids[i]);
				if (user != null)
					roleInfoService.deleteRoleInfo(ids[i]);
			} catch (Exception e) {
				log.error(e);
				flag=false;
			}
		}
		renderJson(response, toJson("success", flag));
		return null;
	}
	/**
	* 保存
	* @param request
	* @param response
	* @return
	* @throws Exception
	*/
	public ModelAndView save(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		boolean flag=true;
		String moduleSet=request.getParameter("moduleSet");
		String[] moduleSets=moduleSet.split(";");
		String roleName=request.getParameter("roleName");
		String remark=request.getParameter("remark");
		String roleid=request.getParameter("roleid");
		List<RoleModule> dataList=roleModuleService.getRoleModuleByHQL("from RoleModule where roleid='"+roleid+"'");
		roleModuleService.deleteBatchVO(dataList);
		RoleInfo roleInfo=new RoleInfo();
		roleInfo.setRoleName(roleName);
		roleInfo.setRoleid(roleid);
		roleInfo.setRemark(remark);
		try{
			roleInfoService.saveOrUpdateRoleInfo(roleInfo);
			for(String temp : moduleSets){
				if(temp!=""){
					String[] detail=temp.split(",");
					RoleModule rm=new RoleModule();
					rm.setRoleid(roleInfo.getRoleid());
					rm.setModuleid(detail[1]);
					rm.setOperateid(detail[0]);
					roleModuleService.saveOrUpdateRoleModule(rm);
				}
			}
		}catch(Exception e){
			log.error(e);
			flag=false;
		}
		renderJson(response, toJson("success",flag));
		return null;
	}
	/**
	* 检查角色名是否已经存在
	* @param request
	* @param response
	* @return
	* @throws Exception
	*/
	public void check(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		boolean flag=true;
		if(roleInfoService.getRoleInfoByHQL("from RoleInfo where roleName='"+java.net.URLDecoder.decode(request.getParameter("roleName"), "UTF-8")+"'").size()!=0)flag=false;
		renderJson(response,toJson("success",flag));
	}
}
